#ifndef SERVICE_REGISTRY_H
#define SERVICE_REGISTRY_H

#include <string>
#include <vector>
#include <map>
#include <mutex>
#include <memory>
#include "../cache/redis_client.h"

struct ServiceInstance {
    std::string serviceId;
    std::string host;
    int port;
    std::string version;
    std::map<std::string, std::string> metadata;
};

class ServiceRegistry {
public:
    ServiceRegistry(std::shared_ptr<RedisClient> redisClient);
    ~ServiceRegistry();

    // 注册服务
    bool registerService(const ServiceInstance& instance);
    
    // 注销服务
    bool deregisterService(const std::string& serviceId);
    
    // 获取服务实例
    std::vector<ServiceInstance> getServiceInstances(const std::string& serviceName);
    
    // 心跳检测
    void startHeartbeat();
    void stopHeartbeat();

private:
    std::shared_ptr<RedisClient> redisClient_;
    std::map<std::string, ServiceInstance> localRegistry_;
    std::mutex mutex_;
    bool heartbeatRunning_;
    
    // 服务key生成
    std::string generateServiceKey(const std::string& serviceName, const std::string& serviceId);
    
    // 心跳线程
    void heartbeatThread();
    
    // 更新服务TTL
    void updateServiceTTL(const std::string& serviceKey);
};

#endif // SERVICE_REGISTRY_H 